package com.wangzh.concurrent.lock.forkjoin;

import java.util.concurrent.RecursiveTask;

public class MyForkJoin extends RecursiveTask<Long> {
    private Long start;
    private Long end;

    public MyForkJoin(Long start, Long end) {
        this.start = start;
        this.end = end;
    }
    @Override
    protected Long compute() {
        long sum =0;
        if (end-start<10000L) {
            for (long i = start; i <= end; i++) {
                sum+=i;
            }
            return sum;
        }else {
            long middle = (end+start)/2;
            MyForkJoin myForkJoin = new MyForkJoin(start, middle);
            myForkJoin.fork();
            MyForkJoin myForkJoin2 = new MyForkJoin(middle+1, end);
            myForkJoin2.fork();

            return myForkJoin.join()+myForkJoin2.join();
        }

    }


}
